<?php


global $AppUI, $project_id, $df, $canEdit, $m, $tab, $canViewTaskFee, $canCreateTasks, $hasRoleFfifaMana, $canViewExpense;

require_once($AppUI->getModuleClass('tasks'));
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$df = $AppUI->getPref('SHDATEFORMAT');

$perms =& $AppUI->acl();
$canAccessTask = $perms->checkModuleItem( 'tasks', 'access');
$canEditTask = $perms->checkModuleItem( 'tasks', 'edit');
$canDeleteTask = $perms->checkModuleItem( 'tasks', 'delete');
$canAddLog = $perms->checkModuleItem( 'timecard', 'add');
$canEditQuotation = $perms->checkModuleItem( 'quotations', 'edit');
$canEditSalesAmount = $perms->checkModuleItem('taskfee', 'edit');
$msg = NULL;
if (!$canAccessTask) {
	$msg = 'You do not have permission to list these tasks';
}

if ($msg != NULL) {
	$AppUI->setMsg($msg, UI_MSG_ERROR);
	$AppUI->redirect();
}

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('TASK_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('TASK_POST');
	}
	else {
		$_POST = $AppUI->getPOST('TASK_POST');
	}
}
$task_not_quotation = intval( dPgetParam( $_GET, 'task_not_quotation', 0 ) );
////edit quotation va khong co quyen edit Tasks => chi duoc xem tasks chua link voi quotation
//if ($canEditQuotation && !$canEditTask) {
//	$task_not_quotation = 1;
//}


$reference = intval( dPgetParam( $_GET, 'reference', 0 ) );

//do search
$q = new DBQuery();
if ($item_type > 0) {
	$q->addWhere("item_type1 = $item_type");
}
$q->exec();
$q->clear();

//main query for tasks
$q->addTable('tasks');
$q->addWhere('task_project = ' . $reference);
//if user's role is FFIA_Staff, load all tasks but only show button new log for user costcenter task
//if user's role is Operation then filter tasks belong to user's cost center
$needCheckCostCenter = $perms->userHasRole($AppUI->user_id, "Operation");
if ($needCheckCostCenter) {
	$q->addWhere("task_costcenter = {$AppUI->user_costcenter}");
}
$needCheckCostCenter = $perms->userHasRole($AppUI->user_id, "Operation") || $perms->userHasRole($AppUI->user_id, "FFIA_Staff");

$q->addOrder('task_id');
//$q->setLimit($page_limit, $page_offset);
$qTotal = $q->prepare();
//$q->addQuery('distinct tasks.*, a.*, u.unit_name');
$q->addQuery('distinct tasks.*');
$tasks = $q->loadList();

header('Content-Type: text/xml');

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');

$xml->startElement('root');

obj2xml($xml, 'tasks', 'item', $tasks);

$xml->endElement();

echo $xml->outputMemory(true);
?>